//
//  CTabBarController.m
//  cifNews_iphone
//
//  Created by  on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CTabBarController.h"
#define ICON_WIDTH 24
@interface CTabBarController ()

@end

@implementation CTabBarController
-(void)dealloc{
    [btnArray release];
    [itemArray release];
    [super dealloc];
}

- (id)initWithArray:(NSArray *)array
{
    self = [super init];
    if (self) {
        // Custom initialization
        btnArray=[[NSMutableArray alloc] init];
        itemArray = [[NSArray alloc]initWithArray:array]; 
        self.tabBar.hidden = YES;
    }
    return self;
}

- (void)setSelectedItemIndex:(NSInteger)index
{
    self.selectedIndex = index;
    
    if (itemArray) {
        itemWidth = 320.0/[itemArray count];
        
        UIImageView *tabView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 431, 320, 49)];
        tabView.tag=110;
        tabView.image = [UIImage imageNamed:@"bottomtoolbar.png"];
        tabView.userInteractionEnabled = YES;
        [self.view addSubview:tabView];
        [self.view bringSubviewToFront:tabView];
        [tabView release];
        if (selectedImage==nil) {
            selectedImage=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellowlight.png"]];
            [tabView addSubview:selectedImage];
            [selectedImage release];
        }
        UIButton*pBtn;
        selectedImage.center=CGPointMake(itemWidth*(self.selectedIndex+1)-itemWidth/2, tabView.frame.size.height/2);
        for (int i=0; i<[itemArray count]; i++) {
            UIImage* ui=[UIImage imageNamed:[[itemArray objectAtIndex:i] objectForKey:@"icon_u"]];
            UIImage* si=[UIImage imageNamed:[[itemArray objectAtIndex:i] objectForKey:@"icon_s"]];

            UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
            item.frame = CGRectMake(itemWidth*i, 0, itemWidth, tabView.frame.size.height);
            item.tag = 1000+i;
            [item setImage:ui forState:UIControlStateNormal];
            [item setImage:si forState:UIControlStateSelected];
            [item addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchDown];
            [tabView addSubview:item];
            [btnArray addObject:item];
            if (i==0) {
                pBtn=item;
            }
            
        }
        [self selectedItem:pBtn];
    }
}


- (void)dissmissTabBarWithAnimate{
    NSLog(@"%@",self.view.subviews);
    UIImageView* tabImg =(UIImageView*) [self.view viewWithTag:110];
    for (UIView* o in self.view.subviews) {
        if ([o isKindOfClass:NSClassFromString(@"UITransitionView")]) {
            o.frame=CGRectMake(0, 0, 320, 480); 
        }
    }
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    [tabImg setFrame:CGRectMake(0, 431+49, 320, 49)];
    [UIView commitAnimations];
}
- (void)ShowTabBarWithAnimate{
    NSLog(@"%@",self.view.subviews);
    for (UIView* o in self.view.subviews) {
        if ([o isKindOfClass:NSClassFromString(@"UITransitionView")]) {
            o.frame=CGRectMake(0, 0, 320, 431); 
        }
    }
    UIImageView* tabImg =(UIImageView*) [self.view viewWithTag:110];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:0.3f];
    [tabImg setFrame:CGRectMake(0, 431, 320, 49)];
    [UIView commitAnimations];
    
}

- (void)selectedItem:(UIButton *)sender
{
    [self filterBtn:sender];
    self.selectedIndex = sender.tag - 1000;
    CGRect frame = selectedImage.frame;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:.3f];
    selectedImage.center=CGPointMake(itemWidth*(self.selectedIndex+1)-itemWidth/2, frame.size.height/2);
    [UIView commitAnimations];
}

-(void)filterBtn:(UIButton*)sender{
    for (UIButton* b in btnArray) {
        b.selected=NO;
    }
    sender.selected=!sender.selected;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
